package com.omesoft.hksnore.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.omesoft.hksnore.R;
import com.omesoft.hksnore.util.CustomToast;
import com.omesoft.hksnore.util.dialog.MyProgressBarDialogUtil;
import com.omesoft.hksnore.util.json.ReErrorCode;
import com.omesoft.hksnore.util.json.SnorekillerJsonUtil;
import com.omesoft.hksnore.util.myactivity.MyActivity;
import com.omesoft.hksnore.util.titlebar.MyTitlebarUtil;
import com.omesoft.hksnore.util.web.CheckNetwork;
import com.omesoft.hksnore.util.web.DataCheckUtil;

/**
 * Created by Omesoft on 2017/2/17.
 */

public class RegisterActivity extends MyActivity implements View.OnClickListener {

    private EditText mPhoneNum_et;
    private EditText mCode_et;
    private Button mNext_or_GetCode_btn;
    private boolean isInputPhone, isInpurCode = false;
    private boolean isNextOrGetCode = false;//判断button为获取验证码还是下一步
    private boolean isBackwardOver = false;
    private boolean isFrist = true;
    private String timerUintString; // 单位：秒,以及文字
    private MyCountDownTimer timer;

    private static final String TAG = "UserRegisterActivity:";
    private Intent intent = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snore_activity_register);
        initTitleBar();
        initView();
        initHandler();
        init();

    }

    @Override
    protected void init() {
        super.init();

        timerUintString = getResources().getString(R.string.register_get_verfity_code_again);
        timer = new MyCountDownTimer(60000, 1000);

    }

    @Override
    protected void initTitleBar() {

        MyTitlebarUtil.showTitleName(activity, R.string.register_register);
        MyTitlebarUtil.showIbLeft(activity,R.mipmap.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initView() {
        mPhoneNum_et = (EditText) findViewById(R.id.register_et_phone);
        mCode_et = (EditText) findViewById(R.id.register_et_code);
        mNext_or_GetCode_btn = (Button) findViewById(R.id.register_btn_getcode);
        mNext_or_GetCode_btn.setOnClickListener(this);
        mCode_et.setEnabled(false);
        mNext_or_GetCode_btn.setEnabled(false);
        mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button_perss));
        mPhoneNum_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String phone = editable.toString();
                if (phone.length() == 0 || phone.length() != 11) {
                    mCode_et.setEnabled(false);
                    mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button_perss));
                    mNext_or_GetCode_btn.setEnabled(false);
                } else {
                    mCode_et.setEnabled(true);
                    mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button));
                    mNext_or_GetCode_btn.setEnabled(true);
                }

            }
        });
        mCode_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String code = editable.toString();
                if (code.length() == 0 || code.equals("")) {
//                    mNext_or_GetCode_btn.setEnabled(false);
                    isNextOrGetCode = false;
                    btnGetCode();
                } else {

                   btnNext();

                }
            }
        });

    }


    private void btnNext(){
        isNextOrGetCode = true;
        mNext_or_GetCode_btn.setText("下一步");
        mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button));
        mNext_or_GetCode_btn.setEnabled(true);
    }
    private void btnGetCode(){
        if (isBackwardOver) {
        mNext_or_GetCode_btn.setText(R.string.register_get_verfity_code_again);
            return;
        }
       if (isFrist == true){
           mNext_or_GetCode_btn.setText(R.string.register_get_verfity_code);
       }

    }


    @Override
    protected void initHandler() {
        super.initHandler();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case ReErrorCode.ERROR_GET_VERIFY_CODE_SUCCEED:
                        timer.start();
                        MyProgressBarDialogUtil.dismiss();
                        break;
                    case ReErrorCode.ERROR_REGISTER_FAIL:
                        MyProgressBarDialogUtil.dismiss();
                        CustomToast.showToasts(context, (String) msg.obj);
                        break;
                    case ReErrorCode.ERROR_VERIFY_BACKWARD:
                        if (isNextOrGetCode) {
                            btnNext();
                            isFrist = false;
                        } else {
                            isFrist = false;
                            mNext_or_GetCode_btn.setEnabled(false);
                            mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button_perss));
                            mNext_or_GetCode_btn.setText(timerUintString + "(" + msg.obj + ")");
                        }
                        break;
                    case ReErrorCode.ERRCODE_SUCEED:
                        MyProgressBarDialogUtil.dismiss();
                        String phone = mPhoneNum_et.getText().toString();
                        String verifyCode = mCode_et.getText().toString();
                        Intent intent = new Intent(activity, SetPasswordActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("phone", phone);
                        bundle.putString("verifyCode", verifyCode);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        anim =1;
                        timer.cancel();
                        finish();
                        break;
                    default:
                        MyProgressBarDialogUtil.dismiss();
                        break;
                }
            }
        };
    }


    class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            isBackwardOver = false;
            sendMsg(ReErrorCode.ERROR_VERIFY_BACKWARD, millisUntilFinished / 1000);


        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            mNext_or_GetCode_btn.setEnabled(true);
            mNext_or_GetCode_btn.setText(R.string.register_get_verfity_code_again);
            mNext_or_GetCode_btn.setBackground(getResources().getDrawable(R.drawable.login_button));
            isBackwardOver = true;

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_btn_getcode:
                if (isNextOrGetCode) {
                    String pn = mPhoneNum_et.getText().toString();
                    String vc = mCode_et.getText().toString();
                    if (CheckNetwork.checkNetwork(activity)) {
                        if (DataCheckUtil.isPhoneNumberValid(pn)) {
                            SnorekillerJsonUtil.CheckSMSVerifyCodeForRegister(handler, pn, vc);
                        }
                    }


                } else {
                    String phone = mPhoneNum_et.getText().toString();
                    if (CheckNetwork.checkNetwork(activity)) {
                        if (DataCheckUtil.isPhoneNumberValid(phone)) {
                            SnorekillerJsonUtil.GetSMSVerifyCodeForRegister(handler, context, phone, currentIP);
                        }
                    }
                }

                break;
        }
    }
}
